{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import matplotlib.pyplot as plt\n",
    "from lxml import etree\n",
    "\n",
    "class Weather:\n",
    "    def __init__(self, weather):\n",
    "        self.weather = weather\n",
    "\n",
    "    def plot_weather_data(self):\n",
    "        plt.figure(figsize=(10, 5))\n",
    "        dates = [entry['date_time'] for entry in self.weather]\n",
    "        hightTemperture = [entry['hight'] for entry in self.weather]\n",
    "        plt.plot(dates, hightTemperture, marker='o')\n",
    "        plt.title('30day Weather Forecast')\n",
    "        plt.xlabel('Data')\n",
    "        plt.ylabel('Temperature (℃)')\n",
    "        plt.grid(True)\n",
    "        plt.xticks(rotation=45)\n",
    "        plt.tight_layout()\n",
    "        plt.show()\n",
    "\n",
    "class GetWeather:\n",
    "    def __init__(self, url):\n",
    "        self.url = url\n",
    "        self.weather_info = []\n",
    "\n",
    "    def get_weather(self):\n",
    "        headers = {\n",
    "            'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36'\n",
    "        }\n",
    "\n",
    "        resp =  requests.get(self.url,  headers=headers)\n",
    "        resp_html = etree.HTML(resp.text)\n",
    "        resp_list = resp_html.xpath(\"//ul[@class='thrui']/li\")\n",
    "        # print(resp_list)\n",
    "        for li in resp_list:\n",
    "            day_weather_info = {}\n",
    "            day_weather_info ['date_time'] = li.xpath(\"./div[1]/text()\")[0].split(' ')[0]\n",
    "            # print(day_weather_info)\n",
    "            hight = li.xpath(\"./div[2]/text()\")[0]\n",
    "            day_weather_info['hight'] = hight[:hight.find('℃')]\n",
    "            low = li.xpath('./div[3]/text()')[0]\n",
    "            day_weather_info['low'] = low[:low.find('℃')]\n",
    "            weather = li.xpath('./div[4]/text()')[0]\n",
    "            day_weather_info['weather'] = weather\n",
    "            wind = li.xpath('./div[5]/text()')[0]\n",
    "\n",
    "            self.weather_info.append(day_weather_info)\n",
    "        \n",
    "        return self.weather_info\n",
    "\n",
    "\n",
    "getWeather = GetWeather('https://lishi.tianqi.com/xishiqu/202406.html')\n",
    "weather = Weather(getWeather.get_weather())\n",
    "weather.plot_weather_data()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
